<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class VoteComments extends Model
{

    use HasFactory;

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = [
        'vote_id',
        'user_id',
        'sort_id',
        'content',
        'parent_id',
        'is_hot',
        'is_top',
        'is_using',
    ];

    /**
     * 查看列表
     *
     * @param $type
     * @param $vote_id
     *
     * @return null
     */
    public static function findByPage( $type = null, $vote_id = null )
    {

        if ( empty( $vote_id ) ) {
            return null;
        }

        switch ($type) {

            case 'Off':
            case 'On':
                $symbol = '=';
                break;

            // 显示所有
            default:
                $symbol = '<>';
                break;

        }

        return static::where( 'is_using', $symbol, $type )
            ->where( 'vote_id', $vote_id )
            ->orderBy( 'id', 'desc' )
            ->orderBy( 'sort_id', 'desc' )
            ->orderBy( 'updated_at', 'desc' )
            ->paginate( getConf( 'Vote_VIEW_NUM' ) );
    }

    /**
     * 保存数据
     *
     * @param $data
     *
     * @return bool
     */
    public function saveData( $data )
    {
        return $this->create( $this->fill( $data )->toArray() ) ? true : false;
    }

    /**
     * 投票ID
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function vote()
    {
        return $this->belongsTo( 'App\Models\Vote', 'vote_id', 'vote_id' );
    }

    /**
     * 投票ID
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function users()
    {
        return $this->belongsTo( 'App\Models\Users', 'user_id', 'user_id' );
    }

}
